package org.groovymud.object;

import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

import org.groovymud.engine.event.MudEventListener;
import org.groovymud.engine.event.ScopedEvent;

public abstract class ReactiveMudObject<T> extends MudObjectWithNameAndDescription implements MudEventListener {

	List<T> initialBehaviours;
	List<T> heartbeatBehaviours;

	List<T> triggeredBehaviours;
	List<T> beforeEventBehaviours;
	List<T> afterEventBehaviours;

	public ReactiveMudObject() {
		initialBehaviours = Collections.synchronizedList(new LinkedList<T>());
		heartbeatBehaviours = Collections.synchronizedList(new LinkedList<T>());

		triggeredBehaviours = Collections.synchronizedList(new LinkedList<T>());
		beforeEventBehaviours = Collections.synchronizedList(new LinkedList<T>());
		afterEventBehaviours = Collections.synchronizedList(new LinkedList<T>());

	}

	public void initialise() {
		super.initialise();
		doBehaviours(initialBehaviours, null);
	}

	public void heartBeat() {
		doBehaviours(heartbeatBehaviours, null);
	}

	protected abstract void doBehaviours(List<T> behaviours, ScopedEvent event);

	public void onAfterMudEvent(ScopedEvent event) {
		doBehaviours(afterEventBehaviours, event);
	}

	public void onBeforeMudEvent(ScopedEvent event) {
		doBehaviours(beforeEventBehaviours, event);
	}

	public void onMudEvent(ScopedEvent event) {
		doBehaviours(triggeredBehaviours, event);
	}

	public List<T> getInitialBehaviours() {
		return initialBehaviours;
	}

	public void setInitialBehaviours(List<T> initialBehaviours) {
		this.initialBehaviours = initialBehaviours;
	}

	public List<T> getHeartbeatBehaviours() {
		return heartbeatBehaviours;
	}

	public void setHeartbeatBehaviours(List<T> heartbeatBehaviours) {
		this.heartbeatBehaviours = heartbeatBehaviours;
	}

	public List<T> getTriggeredBehaviours() {
		return triggeredBehaviours;
	}

	public void setTriggeredBehaviours(List<T> triggeredBehaviours) {
		this.triggeredBehaviours = triggeredBehaviours;
	}

	public List<T> getBeforeEventBehaviours() {
		return beforeEventBehaviours;
	}

	public void setBeforeEventBehaviours(List<T> beforeEventBehaviours) {
		this.beforeEventBehaviours = beforeEventBehaviours;
	}

	public List<T> getAfterEventBehaviours() {
		return afterEventBehaviours;
	}

	public void setAfterEventBehaviours(List<T> afterEventBehaviours) {
		this.afterEventBehaviours = afterEventBehaviours;
	}
}
